/** 兵营 */
var updatearea = require("./updatearea");
cc.Class({
    extends: cc.Component,

    properties: {
        /** 升级对象 */
        upObj:updatearea,
        /** 行对象 */
        pf_line:cc.Prefab,
        /** 行节点 */
        nd_line:cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.upObj.showInfo(11);
    },

    start () {
        let ct = App.U.city[App.cid], bd = ct.area[10];
        this.nd_line.removeAllChildren();
        for(let k in App.C.troop){
            let tr = App.C.troop[k];
            if(!(tr.race == ct.race && tr.troopslevel <= bd.lev)) continue;
            let obj = cc.instantiate(this.pf_line), bid = 22, fid = 19+tr.troopslevel, num = 0;
            obj.getChildByName("sp_pic").getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(App.path + "troops/" + ct.race + "/" + k + ".png");
            obj.getChildByName("txt_name").getComponent(cc.Label).string = tr.monstername;
            obj.getChildByName("txt_trength").getComponent(cc.Label).string = tr.strength;
            obj.getChildByName("txt_coin").getComponent(cc.Label).string = tr.coin;
            obj.getChildByName("txt_ascope").getComponent(cc.Label).string = tr.ascope;
            obj.getChildByName("txt_res").getComponent(cc.Label).string = "无";
            if(tr.troopslevel < 4) bid = 22;
            else if(tr.troopslevel < 7) bid = 23;
            else bid = 24;
            for(let i=11;i<ct.area.length;i++){
                if(ct.area[i].bid == bid && ct.area[i].fids[fid]){
                    //cc.log("casern", fid, ct.area[i].fids[fid], App.C.alchemy["0"][fid], App.C.alchemy["0"][fid][ct.area[i].fids[fid]-1]);
                    num += App.C.alchemy["0"][fid][ct.area[i].fids[fid]-1][9]|0;
                }
            }
            num = num/tr.strength|0
            if(ct.casern[tr.troopslevel]){
                if(tr.upname) num -= ct.casern[tr.troopslevel].hnum;
                else num -= ct.casern[tr.troopslevel].lnum;
            }
            obj.getChildByName("txt_ren").getComponent(cc.Label).string = num;
            let nd = obj.getChildByName("bt_get");
            nd.tid = tr.id;
            nd.on(cc.Node.EventType.TOUCH_END, this.zmClick, this);
            obj.parent = this.nd_line;
        }
        this.nd_line.height = 128 * bd.lev * 2;
    },

    zmClick(ev){
        let num = ev.target.parent.getChildByName("eb_num").getComponent(cc.EditBox).string|0;
        if(num < 1) return App.showTip("请输入正确的数量！");
        App.net.send([3,1,{cid:App.cid,tid:ev.target.tid,num:num}]);
    },

    // update (dt) {},
});
